<template>
  <div>
    <van-pull-refresh
      v-model="isLoading"
      @refresh="onRefresh"
      :disabled="finished"
    >
      <!-- ArtItem是加载内容的标签,因此要把它包裹住 -->
      <!-- immediate-check 阻止内容刚进入就触发load事件 -->
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
      >
        <!-- 循环生成文章的item项组件{{ channelId }} -->
        <ArtItem v-for="item in artList" :key="item.art_id" :article="item">{{
          artList
        }}</ArtItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getArtListAPI } from '@/api/homeAPI.js'
import ArtItem from '@/components/ArtItem/ArtItem.vue'
export default {
  name: 'ArtList',
  // 用来指定是那个标题的内容
  props: {
    channelId: {
      type: Number,
      required: true
    }
  },
  components: {
    ArtItem
  },
  data () {
    return {
      artList: [],
      // 时间戳。初始的默认值为当前的时间戳
      timestamp: Date.now(),
      loading: false,
      finished: false,
      isLoading: false
    }
  },
  created () {
    this.initArtList()
  },

  methods: {
    async initArtList (isPullDown) {
      const { data: res } = await getArtListAPI(this.channelId, this.timestamp)
      console.log(res)
      // 给timestamp,和artlist赋值
      if (res.message === 'OK') {
        // this.artList = res.data.results
        this.timestamp = res.data.pre_timestamp
        if (isPullDown) {
          this.artList = [...res.data.results, ...this.artList]
          this.isloading = false
        } else { // 把新生成的发在后面,旧的放前面
          this.artList = [...this.artList, ...res.data.results]
          this.Loading = false
        }

        // 每次加载完要把loading变成false,方便下次加载

        // console.log(this.artList)
        if (res.data.pre_timestamp === null) {
          this.finished = true
        }
      }
    },
    onLoad () {
      console.log('上啦加载更多')
      // 每次触发onLoad事件就是要加载新内容
      this.initArtList()
    },
    onRefresh () {
      console.log('下拉加载更多')
      // 每次触发onLoad事件就是要加载新内容
      this.initArtList(true)
    }
  }
}

</script>

<style lang="less" scoped>
</style>
